package com.mo.admin.api.button.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mo.admin.api.button.domain.Button;
import com.mo.admin.api.button.domain.vo.ButtonVO;
import com.mo.admin.api.button.mapper.ButtonMapper;
import com.mo.admin.api.button.service.ButtonService;
import com.mo.admin.common.utils.RequestResponseUtils;
import com.mo.admin.security.utils.JWTConstant;
import com.mo.admin.security.utils.JWTUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 * @description 针对表【sys_button(按钮权限表)】的数据库操作Service实现
 * @createDate 2025-06-25 17:12:48
 */
@Service
public class ButtonServiceImpl extends ServiceImpl<ButtonMapper, Button>
        implements ButtonService {
    @Resource
    JWTUtils jwtUtils;

    /**
     * 每个页面对应的按钮权限，格式如下：
     * "useProTable": ["add", "batchAdd", "export", "batchDelete", "status"],
     * "authButton": ["add", "edit", "delete", "import", "export"]
     */
    @Override
    public Map<String, ArrayList<String>> getAuthButtons() {
        HttpServletRequest request = RequestResponseUtils.getCurrentRequest();
        String token = request.getHeader(JWTConstant.AUTHORIZATION);
        String userId = jwtUtils.getSubject(token);

        List<ButtonVO> authButtons = this.baseMapper.getAuthButtons(userId);
        Map<String, ArrayList<String>> map = new HashMap<>();
        for (ButtonVO button : authButtons) {
            ArrayList<String> auths = map.get(button.getName());
            if (auths == null) {
                auths = new ArrayList<>();
                auths.add(button.getButtonCode());
            } else {
                auths.add(button.getButtonCode());
            }
            map.putIfAbsent(button.getName(), auths);
        }

        return map;
    }
}




